<script>
import {ElMessage} from "element-plus";
import axios  from "axios";
export default {
  data() {
    return {
      selectedRoute: null, // 用于v-model绑定
      routeOptions: [
        { label: '未审核', value: '/uashare' },
        // 其他路由选项
        { label: '已审核', value: '/ashare' },
        // 可以根据需要添加更多路由选项
      ],
      //分页
      currentPage: 1,
      pageSize: 5,
      pageInfo: {},

      formLabelWidth: '200px',
      queryFrm: {                    // 分享帖标题查询对象
        keyword: "",
        word:""

      },
      formShow:{},    //显示审核贴信息
      formUpdate:{},  // 修改审核状态信息对象
      shareData: [], // 分享帖信息列表
      dialogUpdateFormVisible:false,
    }
  },
  methods:{
    changeRoute(value) {
      if (this.selectedRoute) {
        // 确保 this.selectedRoute 是一个有效的路由路径字符串
        this.$router.push(this.selectedRoute);
      } else {
        console.error("Selected route is not defined or invalid.");
      }
    },

    //分页
    // LoadStuPageData() {
    //   this.$http.get("http://localhost:8080/share/page", {
    //     params: {
    //       page: this.currentPage,
    //       size: this.pageSize
    //     }
    //   }).then((res) => {
    //     this.pageInfo = res.data;
    //     this.tableData = res.data.list;
    //     console.log(this.tableData);
    //   }).catch((err) => {
    //     console.log(err);
    //   });
    // },


    btnShowUpdate(share_id){            // 打开修改对话框,并回显数据
      console.log(share_id);
      this.formUpdate = {...share_id,operator:this.currentAdminName};
      this.dialogUpdateFormVisible = true;
    },
    btnUpdateShareInfo(){               // 提交修改以后得数据
      console.log(this.formUpdate);
      this.$http.post("http://localhost:8080/share/upshare",this.formUpdate).then((res)=>{
        if (res.data===1){
          console.log("修改成功!");
          ElMessage({
            message: '修改成功!',
            type: 'success',
          })
          this.LoadShareData();
          this.formUpdate={};
          this.dialogUpdateFormVisible = false;
        }else {
          console.log("修改失败!");
          ElMessage({
            message: '修改失败!',
            type: 'warning',
          })
        }
      }).catch((err)=>{})
    },
    onSubmit(){
      console.log(this.queryFrm);
      this.LoadShareData();
    },
    LoadShareData(){
      // let params = {};
      // if (this.queryFrm.keyword){
      //   params.keyword = this.queryFrm.keyword;
      // }
      // if (this.queryFrm.word) {
      //   params.word = this.queryFrm.word;
      // }
      this.$http.get("http://localhost:8080/share/querytitleAcate",{params:{
          keyword:this.queryFrm.keyword,
          word:this.queryFrm.word
        }}).then((res)=>{
        this.shareData = res.data;
        console.log(res.data);
      }).catch(function(err){
        console.log(err);
      })
    },
    LoadShareInfoData(){
      this.$http.get("http://localhost:8080/share/queryShareSta",{params:{
          Share_status:'审核中'
        }}).then((res)=>{
        this.shareData = res.data;
        console.log(res.data)
      }).catch(function (err){
        console.log(err);
      })
    },
  },
  mounted() {
    // this.LoadStuPageData();                  //分页
    this.currentAdminName = localStorage.getItem('adminName')
    console.log(this.currentAdminName)
    this.LoadShareInfoData();
  }
}
</script>

<template>
  <el-card style="max-width: 100%">
    <template #header>
      <div class="card-header">
        <span>分享贴审核(未审核)</span>
      </div>
    </template>
    <el-form :inline="true" :model="queryFrm" class="demo-form-inline">
        <el-form-item>
          <el-select v-model="selectedRoute" placeholder="切换审核状态" style="width: 200px;" @change="changeRoute">
            <el-option
                v-for="item in routeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
          <!--        <el-button type="primary" @click="changeRoute" style="margin-left: 10px;">切换</el-button>-->
        </el-form-item>
      <el-form-item label="标题查询">
        <el-input v-model="queryFrm.keyword" placeholder="请输入标题" clearable />
      </el-form-item>
      <el-form-item label="类别查询">
        <el-input v-model="queryFrm.word" placeholder="请输入类别" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="shareData" stripe style="width: 100%">
      <el-table-column  label="编号" prop="share_id" width="60" />
      <el-table-column prop="nick_name" label="分享人" width="90" />
      <el-table-column prop="share_title" label="标题" />
      <el-table-column prop="share_image" label="图片" />
      <el-table-column prop="share_content" label="内容" />
      <el-table-column prop="share_catagory" label="类别" />
      <el-table-column prop="share_time" label="分享时间" />
      <el-table-column prop="share_status" label="审核状态"/>
<!--      <el-table-column prop="beizhu" label="备注" />-->
<!--      <el-table-column prop="operator" label="审核人"/>-->
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="scope">
          <el-button type="primary" size="small" @click="btnShowUpdate(scope.row)">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-dialog v-model="dialogUpdateFormVisible" title="审核分享贴" width="1000">
    <el-form :model="formUpdate">
      <el-form-item label="编号" :label-width="formLabelWidth">
        {{formUpdate.share_id}}
      </el-form-item>
      <el-form-item label="分享人" :label-width="formLabelWidth">
        {{formUpdate.nick_name}}
      </el-form-item>
      <el-form-item label="标题" :label-width="formLabelWidth">
        {{formUpdate.share_title}}
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <img :src="formUpdate.share_image" class="share-image" />
      </el-form-item>
      <el-form-item label="内容" :label-width="formLabelWidth">
        {{formUpdate.share_content}}
      </el-form-item>
      <el-form-item label="类别" :label-width="formLabelWidth">
        {{formUpdate.share_catagory}}
      </el-form-item>
      <el-form-item label="分享日期" :label-width="formLabelWidth">
        {{formUpdate.share_time}}
      </el-form-item>
      <el-form-item label="审核状态" :label-width="formLabelWidth">
        <el-select v-model="formUpdate.share_status" >
          <el-option label="通过" value="通过"/>
          <el-option label="不通过" value="不通过"/>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="formUpdate.beizhu" autocomplete="off" />
      </el-form-item>
      <el-form-item label="审核人" :label-width="formLabelWidth">
        {{ formUpdate.operator }}
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogUpdateFormVisible = false">取消</el-button>
        <el-button type="primary" @click="btnUpdateShareInfo">
          修改
        </el-button>
      </div>
    </template>
  </el-dialog>

  <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 4, 5, 6]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />

</template>

<style scoped>
.share-image {
  width: 100px;        /* 或者使用 max-width 来适应容器宽度 */
  height: auto;        /* 保持图片的宽高比 */
  border: 1px solid #ddd; /* 给图片添加边框，便于看出图片范围 */
}
</style>